import request, { get, post, put, del } from '@/utils/request.js'
const defaultApi = (apiName) => {
  return {
    list: (params) => get(`/${apiName}/list`, params),
    page: (params) => get(`/${apiName}`, params),
    read: (id) => get(`/${apiName}/` + id),
    create: (id, data) => post(`/${apiName}`, {}, data),
    update: (id, data) => put(`/${apiName}/` + id, {}, { ...data, create_time: undefined, id: undefined }),
    delete: (id) => del(`/${apiName}/` + id)
  }
}
export const http = {
  user: {
    ...defaultApi('user'),
    apply: (data) => post('/user/apply', {}, data),
    updatePassword: (data) => post('/user/updatePassword', {}, data)
  },
  news: defaultApi('news'),
  banner: defaultApi('banner'),
  ship: {
    ...defaultApi('ship'),
    detailByCode: (params) => get('/ship/detailByCode', params)
  },
  order: {
    ...defaultApi('order'),
    cancel: (id) => post('/order/cancel', {}, { id: id }),
  },
  shipCycle: {
    ...defaultApi('cycle'),
    createCustom: (data) => post('/cycle/createCustom', {}, data),
  },
  coupon: defaultApi('coupon'),
  contact: defaultApi('contact'),
  login: (data) =>  post('/user/login', {}, data),
  apply: (data) =>  post('/user/apply', {}, data),
  gallery: defaultApi('gallery'),
}

export const uploadFile = (file) => {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/upload/file',
    method: 'post',
    data: formData
  })
}
